/*
Simple Forum Auto Poster.
Copyright (C) 2011 Gianni Van Hoecke.

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package gui.history;

import domain.DomainController;
import domain.forumdata.Post;
import java.util.List;
import javax.swing.DefaultListModel;

/**
 *
 * @author Gianni Van Hoecke <gianni.vh@gmail.com>
 */
public class HistoryList extends javax.swing.JPanel
{
    private DefaultListModel model;
    
    /** Creates new form HistoryList */
    public HistoryList()
    {
        initComponents();
        model = new DefaultListModel();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        scrollPane = new javax.swing.JScrollPane();
        lstHistory = new javax.swing.JList();
        cmdRemove = new javax.swing.JButton();
        lblInfo = new javax.swing.JLabel();

        setLayout(new java.awt.BorderLayout());

        lstHistory.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "My post title goes here..." };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        lstHistory.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        lstHistory.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                lstHistoryValueChanged(evt);
            }
        });
        scrollPane.setViewportView(lstHistory);

        add(scrollPane, java.awt.BorderLayout.CENTER);

        cmdRemove.setIcon(new javax.swing.ImageIcon(getClass().getResource("/data/remove_32.png"))); // NOI18N
        cmdRemove.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdRemoveActionPerformed(evt);
            }
        });
        add(cmdRemove, java.awt.BorderLayout.PAGE_END);

        lblInfo.setText("<html>Select a history entry in the list below.</html>");
        add(lblInfo, java.awt.BorderLayout.PAGE_START);
    }// </editor-fold>//GEN-END:initComponents

    private void cmdRemoveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdRemoveActionPerformed

        if(this.lstHistory.getSelectedIndex() >= 0)
            DomainController.getInstance().removeHistoryItem((String)this.lstHistory.getSelectedValue());

    }//GEN-LAST:event_cmdRemoveActionPerformed

    private void lstHistoryValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_lstHistoryValueChanged

        if(!evt.getValueIsAdjusting())
        {
            DomainController.getInstance().loadHistoryItem((String)this.lstHistory.getSelectedValue());
        }

    }//GEN-LAST:event_lstHistoryValueChanged


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cmdRemove;
    private javax.swing.JLabel lblInfo;
    private javax.swing.JList lstHistory;
    private javax.swing.JScrollPane scrollPane;
    // End of variables declaration//GEN-END:variables

    public void setButtonsEnabled(boolean enabled)
    {
        this.cmdRemove.setEnabled(enabled);
        if(enabled)
            this.cmdRemove.setEnabled(model.size() > 0);
    }

    public void loadHistory(List<Post> historyList)
    {
        model = new DefaultListModel();
        for(Post post : historyList)
        {
            model.addElement(post.getDisplayTitle());
        }
        this.lstHistory.setModel(model);

        this.cmdRemove.setEnabled(model.size() > 0);
    }
}